//
//  FoundController.m
//  bmob
//
//  Created by hjl on 16/4/4.
//  Copyright © 2016年 hjl. All rights reserved.
//

#import "FoundController.h"
#import <BmobSDK/Bmob.h>
#import "News.h"
#import "AddFoundController.h"

@interface FoundController ()

/** infoMutableArray */
@property (nonatomic, strong) NSMutableArray *infoMutableArray;

- (IBAction)add:(id)sender;

@end

@implementation FoundController

- (void)viewDidLoad {
    [super viewDidLoad];
    _infoMutableArray = [[NSMutableArray alloc] initWithCapacity:1];
    
    NSString *className = @"Found";
    
    BmobQuery *query = [BmobQuery queryWithClassName:className];
    [query orderByDescending:@"updatedAt"];
    
    query.limit = 20;
    [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        
        for (BmobObject *obj in array) {
            News *info    = [[News alloc] init];
            if ([obj objectForKey:@"title"]) {
                info.title    = [obj objectForKey:@"title"];
            }
            if ([obj objectForKey:@"describe"]) {
                info.content  = [obj objectForKey:@"describe"];
            }
            if ([obj objectForKey:@"phone"]) {
                info.phoneNum = [obj objectForKey:@"phone"];
            }
            
            NSLog(@"---> %@", info);

            
            [self.infoMutableArray addObject:info];
        }
        
        NSLog(@"###--> %@", self.infoMutableArray);
        [self.tableView reloadData];
    }];
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.infoMutableArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    News *model = [self.infoMutableArray objectAtIndex:indexPath.row];
    
    cell.textLabel.text = model.title;
    cell.detailTextLabel.text = model.phoneNum;
    
    return cell;
}


- (IBAction)add:(id)sender {
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    AddFoundController *addFoundVc = [sb instantiateViewControllerWithIdentifier:@"addFound"];
    [self.navigationController pushViewController:addFoundVc animated:YES];
    
    addFoundVc.block = ^(BmobObject *obj){
        News *info    = [[News alloc] init];
        if ([obj objectForKey:@"title"]) {
            info.title    = [obj objectForKey:@"title"];
        }
        if ([obj objectForKey:@"describe"]) {
            info.content  = [obj objectForKey:@"describe"];
        }
        if ([obj objectForKey:@"phone"]) {
            info.phoneNum = [obj objectForKey:@"phone"];
        }
        
        [self.infoMutableArray addObject:info];
        
        [self.tableView reloadData];
    };
    
    
    
}
@end
